package com.mvp.lt.airlineview.record;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mvp.lt.airlineview.App;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.utils.AndroidUtil;
import com.mvp.lt.airlineview.utils.SPManager;


/**
 * @author LiuTao
 * @description:录制设置界面 * @date : 2020/10/14 9:45
 */
public class RecordingVideoPopupView extends Dialog {
    //推流分辨率
    public static String RECORD_RESOVEL_POWER = "RECORD_RESOLVE_POWER";
    //静音
    public static String RECORD_SILENCE = "RECORD_SILENCE";
    public int recordWith;
    public int recordHeight;
    private CheckBox mCheckBoxMute;
    private int targetResolution = 0;
    private boolean isSilence = false;
    private Button mRecordBtn;
    private static RecordingVideoPopupView basePopupView;

    private OnPopupListener listener;

    public void setListener(OnPopupListener listener) {
        this.listener = listener;
    }

    public interface OnPopupListener {
        void onStartRecord(int width, int height, boolean audio);
    }


    public RecordingVideoPopupView(Context context) {
        super(context, R.style.base_customDialog);
        Window window = getWindow();
        window.setWindowAnimations(R.style.base_BaseDialogWindowStyle);
    }



    public void setDCancelable(boolean flag) {
        setCancelable(flag);
        setCanceledOnTouchOutside(flag);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_record_vedio);
        DisplayMetrics dm = new DisplayMetrics();

        initUI();
    }


    @Override
    public void show() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        super.show();
        fullScreenImmersive(getWindow().getDecorView());
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }


    private void initUI() {
        targetResolution = SPManager.getInt(SPManager.SP_SPALSH_FLAG, RECORD_RESOVEL_POWER, 0);
        isSilence = SPManager.getBoolean(SPManager.SP_SPALSH_FLAG, RECORD_SILENCE, false);
        RadioGroup resolutionGroup = (RadioGroup) findViewById(R.id.resolution_group);
        RadioButton mResBDButton = (RadioButton) findViewById(R.id.record_radiobutton1);
        RadioButton mResHDButton = (RadioButton) findViewById(R.id.record_radiobutton2);
        RadioButton mResSPDButton = (RadioButton) findViewById(R.id.record_radiobutton3);
        mCheckBoxMute = (CheckBox) findViewById(R.id.check_mute);
        mRecordBtn = (Button) findViewById(R.id.record_video);
        mCheckBoxMute.setChecked(isSilence);
        mCheckBoxMute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPManager.saveBoolean(SPManager.SP_SPALSH_FLAG, RECORD_SILENCE, mCheckBoxMute.isChecked());
            }
        });
        resolutionGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.record_radiobutton1:
                        recordWith = 1280;
                        recordHeight = 720;
                        SPManager.saveInt(SPManager.SP_SPALSH_FLAG, RECORD_RESOVEL_POWER, 0);
                        break;
                    case R.id.record_radiobutton2:
                        recordWith = 1920;
                        recordHeight = 1080;
                        SPManager.saveInt(SPManager.SP_SPALSH_FLAG, RECORD_RESOVEL_POWER, 1);
                        break;
                    case R.id.record_radiobutton3:
                        recordWith = AndroidUtil.getScreenWidth(App.getContext());
                        recordHeight = AndroidUtil.getScreenHeight(App.getContext());
                        SPManager.saveInt(SPManager.SP_SPALSH_FLAG, RECORD_RESOVEL_POWER, 2);
                        break;
                    default:
                        break;

                }
            }
        });
        switch (targetResolution) {
            case 0:
                mResBDButton.setChecked(true);
                recordWith = 1280;
                recordHeight = 720;
                break;
            case 1:
                mResHDButton.setChecked(true);
                recordWith = 1920;
                recordHeight = 1080;
                break;
            case 2:
                mResSPDButton.setChecked(true);
                recordWith = AndroidUtil.getScreenWidth(App.getContext());
                recordHeight = AndroidUtil.getScreenHeight(App.getContext());
                break;
            default:
        }

        mRecordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                //
                if (listener != null) {
                    listener.onStartRecord(recordWith, recordHeight, mCheckBoxMute.isChecked());
                }
            }
        });
    }


    private void fullScreenImmersive(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(uiOptions);
        }
    }

}
